﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SFWS.XHandler.Core;
using JKF.Substructure;
using JKF.Entities.Model;
using JKF.BusinessLayer.BLL;
using System.Data;
using JKF.Entities;

namespace JKF.HandlerLayer.SMS
{
    public partial class Sms_AccountHandler : AHttpHandler
    {
        public override void ProcessRequest()
        {
            //所有错误和异常将抛出异常.正常执行程序则在方法则返回数据
            try
            {
                this.Manager(Utils.GetActionName);
            }
            catch (Exception ex)
            {
                //将错误抛到客户端(以Json格式)
                Utils.ToClient(true, ex.Message);
                //写日志
                AppLogger.Log(ex.Message, ex);
            }
        }

        public void GetSMSAccounts()
        {
            bllSMS_ACCOUNT smsAccount = new bllSMS_ACCOUNT();
            var obj = new PagerList();

            int pageCount = 0;
            int rowCount = 0;

            obj.Entity = from a in smsAccount.GetSMSAccounts(P("searchItem"), P("matchWay"), P("keyword"), P("PageIndex").ToInt32(), P("PageSize").ToInt32(), ref pageCount, ref rowCount).AsEnumerable()
                         select new
                         {
                             SHOP_CODE = a.Field<string>("SHOP_CODE"),
                             SHOP_NAME = a.Field<string>("SHOP_NAME"),
                             LINKMAN = a.Field<string>("LINKMAN"),
                             SMS_ACCOUNT = a.Field<string>("SMS_ACCOUNT"),
                             AMOUNT = a.Field<Int32>("AMOUNT"),
                             NUM_SENDED = a.Field<Int32>("NUM_SENDED"),
                             NUM_LEFT = a.Field<Int32>("NUM_LEFT"),
                             STATUS = a.Field<string>("STATUS"),
                             STATUS_CN = a.Field<string>("STATUS")=="0"?"未开通":"已开通",
                             DATE_CREATE = a.Field<string>("DATE_CREATE"),
                             SIGNATURE = a.Field<string>("SIGNATURE")
                         };

            obj.RowCount = rowCount;
            obj.PageCount = pageCount;
            ResponseJsonObject = obj;
        }

        public void OpenOrCloseAccount()
        {
            bllSMS_ACCOUNT smsAccount = new bllSMS_ACCOUNT();
            var shopCode = P("SHOP_CODE"); //店铺编码
            var account = P("Account"); //短信账号

            try
            {
                smsAccount.OperOrCloseAccount(shopCode, account);
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Message = "操作成功.", Data = shopCode }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }
    }
}
